package com.geovis.emergency.spd.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.system.service.ISystemRegionService;
import com.geovis.emergency.spd.biz.system.wrapper.SystemRegionWrapper;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.geovis.emergency.spd.entity.system.entity.SystemRegion;
import com.geovis.emergency.spd.entity.system.pojo.node.TreeNode;
import com.geovis.emergency.spd.entity.system.pojo.vo.SystemRegionVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 行政区划表 控制器
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/region")
@Api(value = "行政区划", tags = "行政区划")
public class SystemRegionController extends BaseController<ISystemRegionService> {
    /**
     * 详情
     */
    @GetMapping("/getDetail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入region")
    public Result<SystemRegionVO> detail(SystemRegion region) {
        SystemRegion detail = baseService.getOne(Wrappers.lambdaQuery(region));
        return Result.ok(SystemRegionWrapper.build().entityVO(detail));
    }

    /**
     * 分页 行政区划表
     */
    @GetMapping("/listPage")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入region")
    public Result<PageResult<SystemRegion>> list(@RequestBody @Validated PageParam<SystemRegion> pageParam) {
        IPage<SystemRegion> page = pageParam.buildPage();
        LambdaQueryWrapper<SystemRegion> wrapper = Wrappers.lambdaQuery(pageParam.getQuery());
        baseService.page(page, wrapper);
        return Result.ok(new PageResult<SystemRegion>(page));
    }

    /**
     * 懒加载列表
     */
    @GetMapping("/lazyList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "懒加载列表", notes = "传入menu")
    public Result<List<SystemRegionVO>> lazyList(String parentCode,  @RequestParam Map<String, Object> menu) {
        List<SystemRegionVO> list = baseService.lazyList(parentCode, menu);
        return Result.ok(SystemRegionWrapper.build().listNodeLazyVO(list));
    }

    /**
     * 懒加载列表
     */
    @GetMapping("/lazyTree")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "ignoreFirst", value = "忽略第一级", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "joinFirst", value = "拼接第一级", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "懒加载列表", notes = "传入menu")
    public Result<List<TreeNode>> lazyTree(String parentCode, @RequestParam Map<String, Object> menu) {
        List<TreeNode> list = baseService.lazyTree(parentCode, menu);
        List<TreeNode> systemRegionList = SystemRegionWrapper.build().listNodeLazyNodeVO(list);
        String code = menu.get("code").toString();
        String joinFirst = menu.get("joinFirst").toString();
        String ignoreFirst = menu.get("ignoreFirst").toString();
        if (StrUtil.isNotBlank(code) && StatusEnum.TRUE.getCode().equals(ignoreFirst) && StatusEnum.TRUE.getCode().equals(joinFirst)){
            SystemRegion firstRegion = baseService.getOne(
                    Wrappers.<SystemRegion>lambdaQuery().eq(SystemRegion::getCode, code)
                            .last("limit 1")
            );
            if (ObjectUtil.isNotNull(firstRegion)){
                TreeNode treeNode = BeanUtil.toBean(firstRegion, TreeNode.class);
                treeNode.setTitle(firstRegion.getName());
                treeNode.setValue(firstRegion.getCode());
                treeNode.setId(firstRegion.getCode());
                treeNode.setHasChildren(false);
                systemRegionList.add(0,treeNode);
            }
        }
        return Result.ok(systemRegionList);
    }

    /**
     * 新增 行政区划表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "新增", notes = "传入region")
    public Result save(@Valid @RequestBody SystemRegion region) {
        baseService.submit(region);
        return Result.ok();
    }

    /**
     * 修改 行政区划表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "修改", notes = "传入region")
    public Result update(@Valid @RequestBody SystemRegion region) {
        baseService.submit(region);
        return Result.ok();
    }


    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }

    /**
     * 行政区划下拉数据源
     */
    @GetMapping("/select")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "下拉数据源", notes = "传入tenant")
    public Result<List<SystemRegion>> select(@RequestParam(required = false, defaultValue = "00") String code) {
        List<SystemRegion> list = baseService.list(Wrappers.<SystemRegion>query().lambda().eq(SystemRegion::getParentCode, code));
        return Result.ok(list);
    }
}
